#include "icgobject.h"
#include <GL/gl.h>

ICgObject::ICgObject()
{
    transformation_ = Matrix::identity();
}

ICgObject::~ICgObject()
{
    std::vector<Point*>::iterator it;
    for( it = vertices_.begin() ; it != vertices_.end() ; it++ ) {
        delete *it;
    }
}

void ICgObject::multiplyCurrentTransformation(const Matrix &m)
{
    transformation_ = m * transformation_;
//    transformation_ = transformation_ * m;
}

void ICgObject::applyCurrentTransformation()
{
    std::vector<Point*>::iterator it;
    for( it = vertices_.begin() ; it != vertices_.end() ; it++ ) {
        Point *p = *it;
        *p = transformation_ * (*p);
    }
}

void ICgObject::setup_gl_materials() const
{
    GLfloat gl_ambient[] = {material_.ambient().r(), material_.ambient().g(), material_.ambient().b(), 1.0};
    GLfloat gl_diffuse[] = {material_.diffuse().r(), material_.diffuse().g(), material_.diffuse().b(), 1.0};
    GLfloat gl_specular[] = {material_.specular().r(), material_.specular().g(), material_.specular().b(), 1.0};

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, gl_ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gl_diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, gl_specular);
}
